package com.sumanning.mylibmanger.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DBUtils {

    //声明变量
    public static final String TABLE = "t_book";
    public static final String ID = "_id";
    public static final String NAME = "name";
    public static final String PRICE = "price";
    public SqliteHelper dbHelper;

    //构造函数，作用：传值
    public DBUtils(Context context) {
        // 创建sqliteHelper对象
        dbHelper = new SqliteHelper(context);
    }

    /**
     *   * 保存到数据库,保存数据库是通过id插入的，参数为ContentValues
     */
    public int save(ContentValues values) {
        // 获取数据库
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        long id = db.insert(TABLE, null, values);
        db.close();
        //返回数据库id
        return (int) id;
    }

    /**
     *   * 删除数据库,根据id删除 delete from t_book where id=?; ，参数为id
     *  
     */
    public int delete(int id) {
        int effectNum = 0;// 影响行数
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        //参数一：数据库表明，参数二：删除条件 ，参数三：条件值
        effectNum = db.delete(TABLE, ID + "=?", new String[]{String.valueOf(id)});
        db.close();
        return effectNum;
    }

    /**
     *   * 更新数据库,参数为ContentValues
     *  
     */
    public int update(ContentValues values) {
        // 获取内容提供者的Id
        String id = values.getAsString(ID);
        int effectNum = 0;// 影响行数
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        //参数三：条件。参数四：条件值，这里可以为空
        effectNum = db.update(TABLE, values, ID + "=" + id, null);
        db.close();
        return effectNum;
    }

    /**
     *   * 根据游标查询
     *  
     */
    public Cursor findCursor() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query(TABLE, null, null, null, null, null, null);
// 不能关掉，关掉就不能查询
        return cursor;

    }

    /**
     *   * 根据条件查询
     *  
     */
    public Cursor query(int pageNum, int pageSize) {


        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query(TABLE, null, null, null, null, null, null, " "
                + pageNum + " , " + pageSize + " ");

        return cursor;
    }

    //查总页数
    public int pageNum() {
        Cursor cursorTotal = findCursor();
        return cursorTotal.getCount() / 10;

    }

    /**
     *   * 根据List查询
     *  
     */
    public List<Map<String, Object>> find() {

        List<Map<String, Object>> data = null;
        Map<String, Object> map = null;

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query(TABLE, null, null, null, null, null, null);

        if (cursor.getCount() > 0) {
            data = new ArrayList<Map<String, Object>>();
        }
        // 遍历
        while (cursor.moveToNext()) {
            map = new LinkedHashMap<String, Object>();
            map.put(ID, cursor.getInt(cursor.getColumnIndex(ID)));
            map.put(NAME, cursor.getShort(cursor.getColumnIndex(NAME)));
            map.put(PRICE, cursor.getInt(cursor.getColumnIndex(PRICE)));
            data.add(map);
        }
        return data;
    }

    public void pay(int fromUser, int toUser, int money) {

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.beginTransaction();

        try {

            db.execSQL("update t_book set price=price-" + money + " where _id="
                    + fromUser);

            db.execSQL("update t_book set price=price+" + money + " where _id="
                    + toUser);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
            db.close();

        }
    }
}


